/*
 * Message.java
 *
 * Created on 6 de Agosto de 2005, 14:05
 *
 */

/*
 * This software is distributed under the MIT License
 *
 * Copyright (c) 2005 Alain Becam, Paulo Lopes, Joakim Olsson, and Johan Simonsson - 2005
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this 
 * software and associated documentation files (the "Software"), to deal in the Software 
 * without restriction, including without limitation the rights to use, copy, modify, 
 * merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to the following 
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies
 *  or substantial portions of the Software.
 * 
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE 
 * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

package modulesrepository.chat;

/**
 * A top level container for chat messages. A message can be either from a player or from the system.
 *
 * @author Paulo <pmlopes@gmail.com>
 * @version 1.0
 */
public class Message {
    
    /**
     * A message generated by the system
     */
    public static final int MESSAGE_CONTROL = 1;
    /**
     * A message generated by some player
     */
    public static final int MESSAGE_GENERAL = 0;
    
    private int id;
    private final String from;
    private final String message;
    private final int type;
    
    private static sun.misc.BASE64Encoder b64encoder = new sun.misc.BASE64Encoder();
    
    /**
     * Created a message
     *
     * @param type type of the message
     * @param from who sent the message
     * @param message the text message to be sent
     */
    public Message(int type, String from, String message) {
        this.from = from;
        this.message = message;
        this.type = type;
    }
    
    /**
     * Created a message
     *
     * @param type type of the message
     * @param from who sent the message
     * @param message the text message to be sent
     */
    public Message(int type, String from, String message, boolean encode) {
        this(type, from, b64encoder.encode(message.getBytes()));
    }

    /**
     * Internal ID for this message. Id's are generated by player and chatroom objects to identify which ones have been read or not.
     *
     * @param i the message id
     */
    public void setId(int i) {
        this.id = i;
    }
    
    /**
     * Return the id for this message.
     *
     * @return id of the message
     */
    public int getId() {
        return id;
    }
    
    /**
     * Get who sent this message.
     *
     * @return who sent the message
     */
    public String getFrom() {
        return from;
    }
    
    /**
     * Get the message.
     *
     * @return the text message
     */
    public String getMessage() {
        return message;
    }
    
    /**
     * Get the message type.
     *
     * @return type of the message
     */
    public int getType() {
        return type;
    }
    
    /**
     * Format the message as a String message to be handled by a client.
     *
     * @return a string formated message
     */
    public String format() {
        return type + ":" + from + ":" + message;
    }
}
